package com.dl.mall.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.dl.mall.annotation.MethodInfo;
import com.dl.mall.entity.Category;
import com.dl.mall.entity.Page;
import com.dl.mall.entity.Product;
import com.dl.mall.entity.ProductSpecs;
import com.dl.mall.service.admin.CategoryService;
import com.dl.mall.service.user.ProductService;
import com.dl.mall.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DL
 * @create 2022-03-23 20:48
 */
@Slf4j
@RestController()
@RequestMapping("/product")
@Api(tags="产品类controller")
public class ProductController {

    @Resource
    private ProductService productService;

    @Resource
    private CategoryService categoryService;

    /**
     * 根据 产品id 查询该商品的信息
     * @return 结果
     */
    @ApiOperation(value="根据 产品id 查询该商品的信息",notes = "productId=产品id")
    @MethodInfo
    @GetMapping(value = {"/{productId}"})
    public Result getProductById(@PathVariable String productId){
        System.out.println("productId = " + productId);
        Product product = productService.getProductById(productId);

        System.out.println("product = " + product);
        if (product != null){
            return Result.ok("查询产品成功").data("product", product);
        }else {
            return Result.error("查询产品失败");
        }
    }

    @ApiOperation(value="根据 产品id + 配置 搜索sku",notes = "productId=产品id，productSpecs=产品规格")
    @MethodInfo
    @PostMapping("/specs")
    public Result getProductBySpecs(@RequestBody JSONObject jsonObject){
        String productId = jsonObject.getString("productId");
        String productSpecs = jsonObject.getString("productSpecs");
        System.out.println("productId = " + productId);
        System.out.println("productSpecs = " + productSpecs);
        ProductSpecs product = productService.getProductBySpecs(productId, productSpecs);

        System.out.println("product = " + product);
        if (product != null){
            return Result.ok("查询产品规格成功").data("productSpecs", product);
        }else {
            return Result.error("查询产品规格失败");
        }
    }

    @ApiOperation(value="根据 关键字 查询该商品的信息",notes = "keyWord=产品标题关键字，currentPage=当前页")
    @MethodInfo
    @GetMapping("/")
    public Result getProductByKeyWord(String keyWord, int currentPage){
        System.out.println("keyWord = " + keyWord);
        Page<Product> page = productService.getProductByKeyWord(keyWord, currentPage, 20);

        System.out.println("page = " + page);
        if (page != null){
            return Result.ok("查询产品成功").data("page", page);
        }else {
            return Result.error("查询产品失败");
        }
    }

    @ApiOperation(value="首页展示商品信息")
    @MethodInfo
    @GetMapping("/getIndexProductInfo")
    public Result getIndexProductInfo(){
        List<Product> phoneList = productService.getPhoneByRandom(8);
        //System.out.println("/getIndexProductInfo phoneList = " + phoneList);
        List<Product> computerList = productService.getComputerByRandom(6);
        List<Product> intelligentList = productService.getIntelligentByRandom(6);
        List<Product> accessoryList = productService.getAccessoryByRandom(6);

        List<Category> categoryList = categoryService.getCategory();

        //System.out.println("/getIndexProductInfo computerList = " + computerList);
        if (phoneList != null && computerList != null){
            JSONObject phone = new JSONObject();
            phone.put("title", categoryList.get(0).getCategoryName());
            phone.put("logoImg", categoryList.get(0).getCategoryLogo());
            phone.put("productList", phoneList);

            JSONObject computer = new JSONObject();
            computer.put("title", categoryList.get(1).getCategoryName());
            computer.put("logoImg", categoryList.get(1).getCategoryLogo());
            computer.put("productList", computerList);

            JSONObject intelligent = new JSONObject();
            intelligent.put("title", categoryList.get(2).getCategoryName());
            intelligent.put("logoImg", categoryList.get(2).getCategoryLogo());
            intelligent.put("productList", intelligentList);

            JSONObject accessory = new JSONObject();
            accessory.put("title", categoryList.get(3).getCategoryName());
            accessory.put("logoImg", categoryList.get(3).getCategoryLogo());
            accessory.put("productList", accessoryList);

            return Result.ok("查询产品成功")
                    .data("phone", phone)
                    .data("computer", computer)
                    .data("intelligent", intelligent)
                    .data("accessory", accessory);
        }else {
            return Result.error("查询产品失败");
        }
    }
}
